package com.caiwangdai.app.ui.home.borrow.list

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.layout.SingleLayoutHelper
import com.caiwangdai.app.R
import com.caiwangdai.app.bean.HomeContentB
import com.caiwangdai.app.extension.inflate
import com.caiwangdai.app.extension.isUserLogined
import com.caiwangdai.app.extension.launchLoginPage
import com.caiwangdai.app.extension.setOnClickListenerPro
import com.caiwangdai.app.origin.list.holder.BaseRecyclerViewHolder
import com.caiwangdai.app.origin.web.NormalWebActivity
import com.caiwangdai.app.ui.borrow.BorrowIntentionActivity
import kotlinx.android.synthetic.main.vlayout_home_content.view.*


class ContentAdapter(var context: Activity) : DelegateAdapter.Adapter<BaseRecyclerViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = ViewHolder(parent?.inflate(R.layout.vlayout_home_content))
    override fun onCreateLayoutHelper() = SingleLayoutHelper()
    override fun getItemCount() = 1


    var homeContent: HomeContentB? = null

    override fun onBindViewHolder(holder: BaseRecyclerViewHolder?, position: Int) {
        val contentView = ((holder as ViewHolder).itemView)
        //申请借款
        contentView.tv_apply_for.setOnClickListenerPro {
            if (!context.isUserLogined()) {
                context.launchLoginPage()
            } else {
                context.startActivity(BorrowIntentionActivity.newInstance(context))
            }
        }


        homeContent?.apply {
            contentView.tv_borrow_money.text = info?.max_limit
            contentView.tv_borrow_days.text = info?.days
            contentView.iv_explanation.setOnClickListenerPro {
                var url = info?.desc_url!!
                context.startActivity(NormalWebActivity.newInstance(context,url))
            }
        }


    }

    fun setDataToAdapter(homeContent: HomeContentB) {
        this.homeContent = homeContent
        notifyDataSetChanged()
    }


    open class ViewHolder(itemView: View?) : BaseRecyclerViewHolder(itemView)


}